httr::set_config(httr::config(http_version = 0))
library(ggmap)
## Loading required package: ggplot2
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(ggplot2)
library(gridExtra)
ggmap(get_map(geocode("Canberra")))
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Canberra&key=xxx-TJHG3V6fT4FydpG8nZv_ehbN_Iw
## Source : https://maps.googleapis.com/maps/api/staticmap?center=-35.280937,149.130009&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx-TJHG3V6fT4FydpG8nZv_ehbN_Iw

# enable Geocoding API,Maps Static API!!!
corvallis <- c(lon = -123.2620, lat = 44.5646)

# Get map at different zoom level :
map_5 <- get_map(corvallis, zoom = 5, scale = 1)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=44.5646,-123.262&zoom=5&size=640x640&scale=1&maptype=terrain&language=en-EN&key=xxx-TJHG3V6fT4FydpG8nZv_ehbN_Iw
ggmap(map_5)

corvallis_map <- get_map(corvallis,zoom=13,scale=1)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=44.5646,-123.262&zoom=13&size=640x640&scale=1&maptype=terrain&language=en-EN&key=xxx-TJHG3V6fT4FydpG8nZv_ehbN_Iw
ggmap(corvallis_map)

setwd("~/Desktop/Spatial Analysis")
sales<-readRDS("sales.rds")

head(sales)
## # A tibble: 6 x 20
##     lon   lat  price finished_squaref… year_built date       address city  state
##   <dbl> <dbl>  <dbl>             <int>      <int> <date>     <chr>   <chr> <chr>
## 1 -123.  44.6 267500              1520       1967 2015-12-31 1112 N… CORV… OR   
## 2 -123.  44.6 255000              1665       1990 2015-12-31 1221 N… CORV… OR   
## 3 -123.  44.6 295000              1440       1948 2015-12-31 440 NW… CORV… OR   
## 4 -123.  44.6   5000               784       1978 2015-12-31 2655 N… CORV… OR   
## 5 -123.  44.5  13950              1344       1979 2015-12-31 300 SE… CORV… OR   
## 6 -123.  44.6 233000              1567       2002 2015-12-30 3006 N… CORV… OR   
## # … with 11 more variables: zip <chr>, acres <dbl>, num_dwellings <int>,
## #   class <chr>, condition <chr>, total_squarefeet <int>, bedrooms <int>,
## #   full_baths <int>, half_baths <int>, month <dbl>, address_city <chr>
ggmap(corvallis_map) +
  geom_point(aes(lon, lat), data = sales)

# Map color to year_built
ggmap(corvallis_map) +
  geom_point(aes(lon, lat, color = year_built), data = sales)

# Map color to price / finished_squarefeet
ggmap(corvallis_map) +
  geom_point(
    aes(lon, lat, color = price / finished_squarefeet), 
    data = sales
  )

#satellite map
corvallis <- c(lon = -123.2620, lat = 44.5646)
corvallis_map_sat <- get_map(corvallis,maptype="satellite", zoom = 13)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=44.5646,-123.262&zoom=13&size=640x640&scale=2&maptype=satellite&language=en-EN&key=xxx-TJHG3V6fT4FydpG8nZv_ehbN_Iw
ggmap(corvallis_map_sat) +
  geom_point(aes(lon, lat, color = year_built), data = sales)

corvallis_map_bw <- get_map(corvallis, source="stamen",maptype="toner",zoom = 13)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=44.5646,-123.262&zoom=13&size=640x640&scale=2&maptype=terrain&key=xxx-TJHG3V6fT4FydpG8nZv_ehbN_Iw
## Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.
# Edit to display toner map
ggmap(corvallis_map_bw) +
  geom_point(aes(lon, lat, color = year_built), data = sales)

# Use base_layer argument to ggmap() to specify data and x, y mappings
ggmap(corvallis_map_bw, base_layer=ggplot(data=sales,aes(lon,lat,color=year_built))) + geom_point(data=sales,aes(lon,lat,color=year_built))

# Use base_layer argument to ggmap() and add facet_wrap()
ggmap(corvallis_map_bw, base_layer = ggplot(sales, aes(lon, lat))) +
  geom_point(aes(color = class)) +
  facet_wrap(~ class)

qmplot(lon,lat,data=sales,geom="point",color=bedrooms)+ facet_wrap(~ month)
## Using zoom = 13...
## Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.

ward_sales <- readRDS("wards.rds")
head(ward_sales)
##   ward       lon      lat group order num_sales avg_price
## 1    1 -123.3128 44.56531   0.1     1       159  311626.9
## 2    1 -123.3122 44.56531   0.1     2       159  311626.9
## 3    1 -123.3121 44.56531   0.1     3       159  311626.9
## 4    1 -123.3119 44.56531   0.1     4       159  311626.9
## 5    1 -123.3119 44.56485   0.1     5       159  311626.9
## 6    1 -123.3119 44.56430   0.1     6       159  311626.9
##   avg_finished_squarefeet
## 1                1609.226
## 2                1609.226
## 3                1609.226
## 4                1609.226
## 5                1609.226
## 6                1609.226
# Add a point layer with color mapped to ward

g1<-ggplot(ward_sales, aes(lon, lat)) + geom_point(aes(color=ward))
g2<-ggplot(ward_sales, aes(lon, lat))+geom_point(aes(color=group))
# Add a path layer with group mapped to group
g3<-ggplot(ward_sales, aes(lon, lat))+geom_path(aes(group = group))
# Add a polygon layer with fill mapped to ward, and group to group
g4<-ggplot(ward_sales, aes(lon, lat))+geom_polygon(aes(fill=ward,group=group))
grid.arrange(g1, g2,g3,g4, nrow = 2)